home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-08-30 | 3.0 KB | 113 lines | [TEXT/GEOL] |
- Item 1635670 30-Aug-89 09:55
-
- From: KEMINK1 Kemink, Joost,APL
-
- To: SALEME Peat Mareick, Lance Saleme,VCA
-
- cc: MACAPP.TECH$ MACAPP Tech
-
- Sub: CoHandler sample
-
- Lance,
-
- I find cohandlers very useful to perform time-consuming tasks in the
- 'background'. For example, if you want to sort numbers, the sorting algorithm
- can be implemented as a state machine that is executed at idle time. Below, I
- have included some code that implements a simple 'Idler'.
-
- To work with it, create and initialize it, passing a posititve integer as the
- argument. This integer specifies how often the idle method of the idler should
- be called. The DoIdle method decrements the fToGo field (which can be
- interpreted as the state vector of the state machine), and optionally removes
- the handler from the cohandler chain (depending on the fRemoveWhenFinished
- boolean). A few months(?) ago, there was a discussion on MacApp.Tech$ about
- handlers removing themselves from the cohandler chain, but I think the method
- described here is valid (has anyone any input on this?).
-
- To install the idler in the cohandler chain, use the Install method, to remove
- it, use Remove.
-
- I have compiled and tested the code with MacApp 2.0b9 and it worked fine for
- me. Information about state vectors and processes can be found in: Tanenbaum,
- Andrew S., Structured Computer Organization. Englewood Cliffs NJ:
- Prentice/Hall International editions
-
- Hope this helps,
-
- Joost
-
- { ------------- Code follows --------------- }
-
- TIdler = OBJECT(TEvtHandler)
- fToGo : INTEGER;
- fInstalled : BOOLEAN;
- fRemoveWhenFinished : BOOLEAN;
-
- PROCEDURE TIdler.IIdler(initialValue:INTEGER);
-
- PROCEDURE TIdler.Install;
- PROCEDURE TIdler.Remove;
-
- FUNCTION TIdler.DoIdle(phase:IdlePhase):BOOLEAN; OVERRIDE;
-
- FUNCTION TIdler.IsFinished:BOOLEAN;
- FUNCTION TIdler.IsInstalled:BOOLEAN;
- END;
-
- PROCEDURE TIdler.IIdler(initialValue:INTEGER);
- BEGIN
- SELF.fToGo:=initialValue;
- SELF.fInstalled:=FALSE;
- SELF.IEvtHandler(NIL);
- SELF.fIdleFreq:=1;
- SELF.fRemoveWhenFinished:=TRUE;
- END;
-
- FUNCTION TIdler.DoIdle(phase:IdlePhase):BOOLEAN; OVERRIDE;
- BEGIN
- {$IFC qDebug}
- Write('TIdler.DoIdle - ');
- {$ENDC qDebug}
- IF NOT SELF.IsFinished THEN
- BEGIN
- SELF.fToGo:=SELF.fToGo-1;
- {$IFC qDebug}
- Writeln('To Go: ',SELF.fToGo:1);
- {$ENDC qDebug}
- END;
- IF SELF.IsFinished AND SELF.fRemoveWhenFinished THEN
- SELF.Remove;
- DoIdle:=FALSE;
- END;
-
- PROCEDURE TIdler.Install;
- BEGIN
- IF NOT SELF.IsInstalled THEN
- BEGIN
- SELF.fInstalled:=TRUE;
- gApplication.InstallCoHandler(SELF,TRUE);
- END;
- END;
-
- PROCEDURE TIdler.Remove;
- BEGIN
- IF SELF.IsInstalled THEN
- BEGIN
- fInstalled:=FALSE;
- gApplication.InstallCoHandler(SELF,FALSE);
- END;
- END;
-
- FUNCTION TIdler.IsFinished:BOOLEAN;
- BEGIN
- IsFinished:=(SELF.fToGo<=0);
- END;
-
- FUNCTION TIdler.IsInstalled:BOOLEAN;
- BEGIN
- IsInstalled:=SELF.fInstalled;
- END;
-
- { ---- End of code ---- }
-
-